<?php
/**
 * 订单表
 */

class Order_model extends CT_Model{
    public $_table = 'order';
    public $_table_book = 'book';
    public $_table_course_package = 'course_package';
    
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    /**
     * 获取订单汇总列表
     */
    public function summary_order_list($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL){
    	try {
    		if ($fields != NULL) {
    			$this->db->select ( $fields );
    		}
    		$this->db->from($this->_table);
    		
    		$this->db->join('member','member.id = order.member_id','join');		//连接用户表
    		//$this->db->join('course_package','course_package.id = order.course_package_id','join');		//连接课包表
    		$this->db->join('nursery','nursery.id = order.nursery_id','join');		//连接园所
    	
    		if ($conditions != NULL && $conditions) {
    			if (is_array ( $conditions )) {
    				$this->db->where ( $conditions );
    			} else {
    				$this->db->where ( $conditions, NULL, FALSE );
    			}
    		}
    		if ($order != NULL) {
    			$this->db->order_by ( $order );
    		}
    		if ($limit != NULL) {
    			$this->db->limit ( $limit, $start );
    		}
    		return $this->db->get()->result_array();
    	}catch (Exception $e){
    	
    	}
    }
    
    public function get_order_package_list($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL) {

        if ($fields != NULL) {
            $this->db->select ( $fields );
        }
        
        $this->db->from( $this->_table );
//         $this->db->join( $this->_table_book, $this->_table_book.'.order_id = '.$this->_table.'.id', 'left');
        $this->db->join( $this->_table_course_package, $this->_table_course_package.'.id = '.$this->_table.'.course_package_id', 'left');
        
        if ($conditions != NULL && $conditions) {
            if (is_array ( $conditions )) {
                $this->db->where ( $conditions );
            } else {
                $this->db->where ( $conditions, NULL, FALSE );
            }
        }
       
    
        if ($order != NULL) {
            $this->db->order_by ( $order );
        }
    
        if ($limit != NULL) {
            $this->db->limit ( $limit, $start );
        }
        
        return $this->db->get()->result_array();
        
    }
    /**
     * 获取总课时
     */
    public function gethours($conditions = NULL){
    	try {
    		$this->db->select ('sum(hours)');
    		$this->db->from( $this->_table );
    		//         $this->db->join( $this->_table_book, $this->_table_book.'.order_id = '.$this->_table.'.id', 'left');
    		//$this->db->join( $this->_table_course_package, $this->_table_course_package.'.id = '.$this->_table.'.course_package_id', 'left');
    		
    		if ($conditions != NULL && $conditions) {
    			if (is_array ( $conditions )) {
    				$this->db->where ( $conditions );
    			} else {
    				$this->db->where ( $conditions, NULL, FALSE );
    			}
    		}
    		
    		$result = $this->db->get()->row_array();
    		if(empty($result))throw new Exception();
    		return array_shift($result);
    		//echo '<pre>';print_r($result);die();
    	} catch (Exception $e) {
    		return 0;
    	}
    }
    /**
     * 获取总金额
     */
    public function getmoney($conditions = NULL){
    	try {
    		$this->db->select ('sum(pay_fee)');
    		$this->db->from( $this->_table );
    		//         $this->db->join( $this->_table_book, $this->_table_book.'.order_id = '.$this->_table.'.id', 'left');
    		//$this->db->join( $this->_table_course_package, $this->_table_course_package.'.id = '.$this->_table.'.course_package_id', 'left');
    
    		if ($conditions != NULL && $conditions) {
    			if (is_array ( $conditions )) {
    				$this->db->where ( $conditions );
    			} else {
    				$this->db->where ( $conditions, NULL, FALSE );
    			}
    		}
    
    		$result = $this->db->get()->row_array();
    		if(empty($result))throw new Exception();
    		return array_shift($result);
    		//echo '<pre>';print_r($result);die();
    	} catch (Exception $e) {
    		return 0;
    	}
    }
    /**
     * 获取总金额
     */
    public function getsurplus_hours($conditions = NULL){
    	try {
    		$this->db->select ('sum(surplus_hours)');
    		$this->db->from( $this->_table );
    		//         $this->db->join( $this->_table_book, $this->_table_book.'.order_id = '.$this->_table.'.id', 'left');
    		//$this->db->join( $this->_table_course_package, $this->_table_course_package.'.id = '.$this->_table.'.course_package_id', 'left');
    
    		if ($conditions != NULL && $conditions) {
    			if (is_array ( $conditions )) {
    				$this->db->where ( $conditions );
    			} else {
    				$this->db->where ( $conditions, NULL, FALSE );
    			}
    		}
    
    		$result = $this->db->get()->row_array();
    		if(empty($result))throw new Exception();
    		return array_shift($result);
    		//echo '<pre>';print_r($result);die();
    	} catch (Exception $e) {
    		return 0;
    	}
    }
    /**
     * 24小时仍未付款的订单变更已过期状态
     * @return mixed
     */
    public function update_order_not_pay()
    {
        $this->db->where('status',0);
        $this->db->where('UNIX_TIMESTAMP(create_date)+60*60*24 <=',time());
        
        return $this->db->update($this->_table, array('status'=>6));
    }
}
